package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


    @Override
    public List<JSONObject> getBaseCategoryList() {
		ArrayList<JSONObject> resultList = new ArrayList<>();
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		Map<Long, List<BaseCategoryView>> categoryMap1 = baseCategoryViews.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = categoryMap1.entrySet().iterator();
		while (iterator.hasNext()) {
			JSONObject category1 = new JSONObject();
			Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator.next();
			List<BaseCategoryView> baseCategoryViews1 = entry1.getValue();
			category1.put("categoryName",baseCategoryViews1.get(0).getCategory1Name());
			category1.put("categoryId", entry1.getKey());

			ArrayList<JSONObject> list2 = new ArrayList<>();
			Map<Long, List<BaseCategoryView>> categoryMap2 = baseCategoryViews1.stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = categoryMap2.entrySet().iterator();
			while (iterator1.hasNext()) {
				JSONObject category2 = new JSONObject();
				Map.Entry<Long, List<BaseCategoryView>> entry2 = iterator1.next();
				Long category2Id = entry2.getKey();
				List<BaseCategoryView> baseCategoryViews2 = entry2.getValue();
				category2.put("categoryName", baseCategoryViews2.get(0).getCategory2Name());
				category2.put("categoryId",category2Id);

				List<JSONObject> list3 = baseCategoryViews2.stream().map(baseCategoryView -> {
					JSONObject category3 = new JSONObject();
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					return category3;
				}).toList();
				category2.put("categoryChild", list3);
				list2.add(category2);
			}
			category1.put("categoryChild", list2);
			resultList.add(category1);
		}

		return resultList;
	}
}
